package com.etooling.framework.service.impl;

import com.etooling.framework.entity.*;
import com.etooling.framework.repository.*;
import com.etooling.framework.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author X
 * @title: CompanyServiceImpl
 * @projectName etooling
 * @description: 帐套服务实现
 * @date 2023/3/713:49
 */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    CompanyRepository companyRepository;
    @Autowired
    OrgRepository orgRepository;

    @Autowired
    FuncModuleRepository funcModuleRepository;

    @Autowired
    RoleRepository roleRepository;

    @Autowired
    EmployeeRepository employeeRepository;

    @Override
    public Company initCompanyData() {
        Integer cnt = companyRepository.getByRootCnt();
        if (cnt == 0) {
            System.out.println("首次使用初始化帐套...");
            Company company = new Company();
            company.setName("系统框架");
            company.setStatus(Company.RUNNING_STATUS);
            company = companyRepository.save(company);
            //创建系统初始化帐号
            Org org = new Org();
            org.setCompanyId(company.getId());
            org.setStatus(Org.RUNNING_STATUS);
            org.setName("易图灵");
            org.setOrgCode("root");
            orgRepository.save(org);
            //菜单功能

            List<FuncModule> adminRolesModules = new ArrayList<>();

            FuncModule sysMgr = funcModuleRepository.save(new FuncModule(company.getId(), "0-0", "系统管理", "目录", "router","root", "", "是", ""));
            adminRolesModules.add(sysMgr);

            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-1", "系统帐号管理", "菜单", "router", sysMgr.getId(), "empolyeeMgr", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-2", "系统参数管理", "菜单", "router", sysMgr.getId(), "dataDir", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-3", "文件存储配置", "菜单", "router", sysMgr.getId(), "fileSource", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-4", "系统模块管理", "菜单", "router", sysMgr.getId(), "fmodule", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-5", "角色管理", "菜单", "router", sysMgr.getId(), "roleMgr", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-6", "机构管理", "菜单", "router", sysMgr.getId(), "orgMrg", "是", "功能窗口")));
            adminRolesModules.add(funcModuleRepository.save(new FuncModule(company.getId(), "0-7", "企业账套授权管理", "菜单", "router", sysMgr.getId(), "companyMgr", "是", "功能窗口")));

            //角色
            Role role = new Role();
            role.setName("超级管理员");
            role.setStatus(Role.RUNNING_STATUS);
            roleRepository.save(role);
            role.setFuncModules(adminRolesModules);
            roleRepository.save(role);
            List<Role> adminRoles = new ArrayList<>();
            adminRoles.add(role);

            //员工
            Employee admin = new Employee();

            admin.setStatus(Employee.RUNNING_STATUS);
            admin.setCreateTime(new Date());
            admin.setPwd("qwer1234");
            admin.setLoginId("admin");
            admin.setCompanyId(company.getId());
            admin.setOrg(org);
            employeeRepository.save(admin);
            admin.setRoles(adminRoles);
            employeeRepository.save(admin);
            System.out.println("系统初始化完成");

        }
        return null;
    }
}
